;
! function() {
	var laypage = layui.laypage,
		form = layui.form;

	to_page(0, 10);

	// 搜索点击事件
	$("#searchBtn").click(function() {
		to_page(0, 10);
	});

	// 回车键搜索
	$("body").keydown(function() {
		if(event.keyCode == "13") {
			to_page(0, 10);
		}
	});
	
	// 选择交易类型后即时查询
	form.on('select(type)', function(data) {
		to_page(0, 10);
	});
	
	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/record/",
			type: "post",
			data: {
				"type": $("#type").val(),
				"orderNo": $("#searchBox").val(),
				"pageNum": pageNum,
				"pageSize": pageSize
			},
			async: false,
			success: function(result) {
				if(result.status == 1 || result.data.totalRow == 0) {
					// 提示没有数据
					$("#record-content").html("<tr><td colspan='7' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装tr并赋给页面
					build_table(result.data);
					if(result.data.totalRow > 10) {
						// 拼装分页总览信息并赋给页面
						build_pageInfo(result.data);
						// 显示分页按钮
						laypage.render({
							elem: 'page_nav_div',
							count: result.data.totalRow,
							curr: result.data.pageNumber,
							limit: result.data.pageSize,
							theme: "#FF6700",
							prev: "<i class='layui-icon'>&#xe65a;</i>",
							next: "<i class='layui-icon'>&#xe65b;</i>",
							layout: ['prev', 'page', 'next', 'limit', 'skip'],
							jump: function(obj, first) {
								if(!first) {
									to_page(obj.curr, obj.limit);
								}
							}
						});
						// 显示分页条
						$("#page_info").show();
					} else {
						// 隐藏分页条
						$("#page_info").hide();
					}
					// 订单号搜索提示
					$.typeahead({
						input: ".js-typeahead",
						order: "asc",
						source: {
							groupName: {
								ajax: {
									url: "/record/getAllOrderNo"
								}
							}
						}
					});
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + (pageInfo.pageNumber == 0 ? 1 : pageInfo.pageNumber) + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#record-content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var orderNoTd = $("<td></td>").append(this.orderNo);
			var typeOfTransactionTd = $("<td></td>").append(this.typeOfTransaction);
			var paymentMethodTd = $("<td></td>").append(this.paymentMethod);
			var orderAmountTd = $("<td></td>").append(this.orderAmount);
			var paymentOrRefundStateTd = $("<td></td>").append(this.paymentOrRefundState);
			var paymentOrRefundTimeTd = $("<td></td>").append(this.paymentOrRefundTime);
			var createTime = $("<td></td>").append(this.createTime);
			// 赋给tbody
			$("<tr class='record-tr'></tr>").append(orderNoTd).append(typeOfTransactionTd)
				.append(paymentMethodTd).append(orderAmountTd).append(paymentOrRefundStateTd)
				.append(paymentOrRefundTimeTd).append(createTime).appendTo("#record-content");
		});
	}

}();